<?php
class Manager {
	
	private static $instance;
	private $connection;
	public function execute(Command $command) {
		try {
			self::openConnection ();
			$result = $command->doExecute ( $this->connection );
			
			if ($this->connection->error) {
				throw new ErrorException ( "boaSqlError", $this->connection->errno );
			}
		} catch ( ErrorException $e ) {
			Logger::getInstance ()->error ( "Error while executing command. ", $e );
			$this->connection->rollback ();
			if ($result != null) {
				$result = null;
			}
		
		}
		$this->closeConnection ();
		return $result;
	}
	
	public static function getInstance() {
		return self::$instance == null ? self::initConnection () : self::$instance;
	}
	
	private static function initConnection() {
		self::$instance = new Manager ( );
		return self::$instance;
	}
	
	private function openConnection() {
		if($this->connection == null){
			$this->connection = new mysqli ( 'localhost', 'root', '1234', 'boa' );
		}else{
			$this->connection->connect('localhost', 'root', '1234', 'boa');
		}
	}
	
	private function closeConnection() {
		if ($this->connection != null) {
			$this->connection->close ();
		}
	}
	
	public function getActiveConnection() {
		return $this->connection;
	}
}

?>